package com.aracky.assess.controller;

import com.aracky.assess.model.entity.HrKpiPostPerformanceConfig;
import com.aracky.assess.model.form.HrKpiPostPerformanceConfigForm;
import com.aracky.assess.model.form.HrKpiPostPerformanceConfigListForm;
import com.aracky.assess.model.request.HrKpiPostPerformanceConfigRequest;
import com.aracky.assess.model.vo.HrKpiPostPerformanceConfigTableVO;
import com.aracky.assess.model.vo.HrKpiPostPerformanceConfigVO;
import com.aracky.assess.service.HrKpiPostPerformanceConfigService;
import com.aracky.common.util.ConvertUtil;
import com.aracky.common.vo.CommonResult;
import com.aracky.common.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 岗位考核指标配置 前端控制器
 * </p>
 *
 * @author zzp
 * @since 2022-08-01 09:43:18
 */
@RestController
@RequestMapping("/hrKpiPostPerformanceConfig")
@Api(tags = "岗位考核指标占比评分接口")
public class HrKpiPostPerformanceConfigController {

    @Resource
    private HrKpiPostPerformanceConfigService hrKpiPostPerformanceConfigService;

    // /**
    //  * 查询出岗位部门考核评分人以及指标详情表信息
    //  * @param hrKpiPostPerformanceConfigRequest
    //  * @return
    //  */
    // @PostMapping("/getDetailTable")
    // @ApiOperation(value = "获取详细信息列表")
    // public CommonResult<PerformanceAssessorTableVO> getDetailTable(@RequestBody HrKpiPostPerformanceConfigRequest hrKpiPostPerformanceConfigRequest){
    //     PerformanceAssessorTableVO performanceAssessorTableVO = hrKpiPostPerformanceConfigService.getDetailTable(hrKpiPostPerformanceConfigRequest);
    //     if (null == performanceAssessorTableVO) {
    //         return CommonResult.error("查询失败！");
    //     }
    //     return CommonResult.success(performanceAssessorTableVO,"查询成功！");
    // }

    // /**
    //  * 分页查询岗位部门列表信息
    //  * @param postDeptRequest
    //  * @return
    //  */
    // @PostMapping("/getPostDept")
    // @ApiOperation(value = "分页查询岗位部门信息列表")
    // public CommonResult<PageVO<PostDeptVO>> getPostDept(@RequestBody PostDeptRequest postDeptRequest){
    //     PageVO<PostDeptVO> pageVO = hrKpiPostPerformanceConfigService.getPostDept(postDeptRequest);
    //     if (null == pageVO) {
    //         return CommonResult.error("查询失败！");
    //     }
    //     return CommonResult.success(pageVO,"查询成功！");
    // }

    /**
     * 保存新增岗位考核指标列表，或者更新岗位考核指标列表
     * @param hrKpiPostPerformanceConfigFormList
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存新增、修改岗位指标列表")
    public CommonResult savePostPerformanceConfig(@RequestBody HrKpiPostPerformanceConfigListForm hrKpiPostPerformanceConfigFormList) {
        // 获取postId并设置postId
        List<HrKpiPostPerformanceConfigForm> hrKpiPostPerformanceConfigForms = hrKpiPostPerformanceConfigFormList.getHrKpiPostPerformanceConfigVOList();
        // 保存进行数据库对象集合
        List<HrKpiPostPerformanceConfig> hrKpiPostPerformanceConfigList = new ArrayList<>(hrKpiPostPerformanceConfigForms.size());
        for (HrKpiPostPerformanceConfigForm hrKpiPostPerformanceConfigForm : hrKpiPostPerformanceConfigForms) {
            hrKpiPostPerformanceConfigForm.setPostId(hrKpiPostPerformanceConfigFormList.getPostId());
            // 将前端接收的对象转成实体类，保存到数据库
            hrKpiPostPerformanceConfigList.add(ConvertUtil.sourceToTarget(hrKpiPostPerformanceConfigForm,HrKpiPostPerformanceConfig.class));
        }
        // 保存岗位配置列表信息（判断是新增还是修改）
        if (null == hrKpiPostPerformanceConfigForms.get(0).getId()) {
            if (hrKpiPostPerformanceConfigService.saveBatch(hrKpiPostPerformanceConfigList)) {
                return CommonResult.success("添加成功！");
            }
            return CommonResult.error("保存失败！");
        }
        // 批量更新失败
        // if (hrKpiPostPerformanceConfigService.updateBatchById(hrKpiPostPerformanceConfigList)) {
        //     return CommonResult.success("修改成功！");
        // }

        try {
            hrKpiPostPerformanceConfigService.updatePostPerformanceConfigList(hrKpiPostPerformanceConfigList);
        } catch (Exception e) {
            return CommonResult.error("修改失败！");
        }

        return CommonResult.success("保存成功！");
    }
    
    // @PostMapping("/save")
    // @ApiOperation(value = "新增")
    // public CommonResult saveInfo(@RequestBody HrKpiPostPerformanceConfigForm hrKpiPostPerformanceConfigForm){
    //     HrKpiPostPerformanceConfig hrKpiPostPerformanceConfig= ConvertUtil.sourceToTarget(hrKpiPostPerformanceConfigForm,HrKpiPostPerformanceConfig.class);
    //     hrKpiPostPerformanceConfigService.save(hrKpiPostPerformanceConfig);
    //     return CommonResult.success("新增成功!");
    // }
    //
    // @PutMapping("/update")
    // @ApiOperation(value = "修改")
    // public CommonResult updateInfo(@RequestBody HrKpiPostPerformanceConfigForm hrKpiPostPerformanceConfigForm){
    //     HrKpiPostPerformanceConfig hrKpiPostPerformanceConfig= ConvertUtil.sourceToTarget(hrKpiPostPerformanceConfigForm,HrKpiPostPerformanceConfig.class);
    //     hrKpiPostPerformanceConfigService.updateById(hrKpiPostPerformanceConfig);
    //     return CommonResult.success("修改成功!");
    // }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "ids",value = "表id")
    public CommonResult deleteInfo(Long[] ids){
        List<Long> list=new ArrayList<>();
        for (Long id:ids){
            list.add(id);
        }
        hrKpiPostPerformanceConfigService.removeBatchByIds(list);
        return CommonResult.success("删除成功!");
    }

    @GetMapping("/getInfo")
    @ApiOperation(value = "查看详情")
    public CommonResult<HrKpiPostPerformanceConfigVO> getInfo(@RequestParam("id") Long id){
        HrKpiPostPerformanceConfig hrKpiPostPerformanceConfig=hrKpiPostPerformanceConfigService.getById(id);
        HrKpiPostPerformanceConfigVO hrKpiPostPerformanceConfigVO=ConvertUtil.sourceToTarget(hrKpiPostPerformanceConfig,HrKpiPostPerformanceConfigVO.class);
        return CommonResult.success(hrKpiPostPerformanceConfigVO,"查询详情成功!");
    }

    // /**
    //  * 根据条件分页查询 postId(可以扩展参数)
    //  * @param hrKpiPostPerformanceConfigRequest
    //  * @return
    //  */
    @PostMapping("/getPageList")
    @ApiOperation(value = "查询岗位指标列表(分页)")
    public CommonResult<PageVO<HrKpiPostPerformanceConfigVO>> getPageList(@RequestBody HrKpiPostPerformanceConfigRequest hrKpiPostPerformanceConfigRequest){
        PageVO<HrKpiPostPerformanceConfigVO> pageVO = hrKpiPostPerformanceConfigService.getPageList(hrKpiPostPerformanceConfigRequest);
        if (pageVO == null){
            return CommonResult.error("查询失败！");
        }
        return CommonResult.success(pageVO,"查询成功！");
    }

    // /**
    //  * 根据postId查询(可以扩展参数)
    //  * @param hrKpiPostPerformanceConfigRequest
    //  * @return
    //  */
    // @PostMapping("/getList")
    // @ApiOperation(value = "查询岗位指标列表(不分页)")
    // public CommonResult<HrKpiPostPerformanceConfigTableVO> getList(@RequestBody HrKpiPostPerformanceConfigRequest hrKpiPostPerformanceConfigRequest){
    //
    //     HrKpiPostPerformanceConfigTableVO hrKpiPostPerformanceConfigTableVO = new HrKpiPostPerformanceConfigTableVO();
    //     List<HrKpiPostPerformanceConfigVO> hrKpiPostPerformanceConfigVOList = hrKpiPostPerformanceConfigService.getList(hrKpiPostPerformanceConfigRequest);
    //     HrKpiPostPerformanceConfigVO hrKpiPostPerformanceConfigVO = hrKpiPostPerformanceConfigVOList.get(0);
    //     hrKpiPostPerformanceConfigTableVO.setDeptName(hrKpiPostPerformanceConfigVO.getDeptName());
    //     hrKpiPostPerformanceConfigTableVO.setPostName(hrKpiPostPerformanceConfigVO.getPostName());
    //     hrKpiPostPerformanceConfigTableVO.setPostId(hrKpiPostPerformanceConfigVO.getPostId());
    //
    //     if (null == hrKpiPostPerformanceConfigVOList){
    //         return CommonResult.error("查询失败！");
    //     }
    //     return CommonResult.success(hrKpiPostPerformanceConfigTableVO,"查询成功！");
    // }

    /**
     * 根据postId查询
     * @param postId
     * @return
     */
    @GetMapping("/getListByPostId")
    @ApiOperation(value = "根据postId查询岗位指标列表(不分页)")
    public CommonResult<HrKpiPostPerformanceConfigTableVO> getListByPostId(@RequestParam("postId") Long postId){
        HrKpiPostPerformanceConfigTableVO hrKpiPostPerformanceConfigTableVO = hrKpiPostPerformanceConfigService.getListByPostId(postId);
        if (null == hrKpiPostPerformanceConfigTableVO){
            return CommonResult.error("查询失败！");
        }
        return CommonResult.success(hrKpiPostPerformanceConfigTableVO,"查询成功！");
    }

    //
    // @PostMapping("/pageList")
    // @ApiOperation(value = "列表查询")
    // public CommonResult<PageVO<HrKpiPostPerformanceConfigVO>> list(@RequestBody HrKpiPostPerformanceConfigRequest hrKpiPostPerformanceConfigRequest){
    //      PageVO<HrKpiPostPerformanceConfigVO> pageVO=hrKpiPostPerformanceConfigService.getHrKpiPostPerformanceConfigList(hrKpiPostPerformanceConfigRequest);
    //      return CommonResult.success(pageVO);
    // }
}
